﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NewsAggregatorLibrary.DAL;

namespace NewsAggregatorLibrary
{
    public class FeedsMember : Feeds
    {
        /// <summary>
        /// Retrieves all Feeds from the DB.
        /// </summary>
        /// <returns></returns>
        public static FeedsMemberCollection RetrieveAll()
        {
            try
            {
                return DB.Select().From(Schema).ExecuteAsCustomCollection<Feeds, FeedsCollection, FeedsMember, FeedsMemberCollection>();
            }
            catch (Exception e)
            {
                throw new Exception("An error has occured while generating a list of Feeds " + e.Message, e);
            }
        }

        /// <summary>
        /// Retrieves a feed by ID.
        /// </summary>
        /// <returns></returns>
        public static FeedsMember RetrieveByID(int id)
        {
            try
            {
                return DB.Select().From(Schema).Where(Columns.Id).IsEqualTo(id).ExecuteSingle<FeedsMember>();
            }
            catch (Exception e)
            {
                throw new Exception("An error has occured while generating a list of Feeds " + e.Message, e);
            }
        }

        /// <summary>
        /// Retrieves a feed by CategoryID.
        /// </summary>
        /// <returns></returns>
        public static FeedsMemberCollection RetrieveByCategoryID(int categoryId)
        {
            try
            {
                return DB.Select().From(Schema).Where(Columns.CategoryID).IsEqualTo(categoryId).ExecuteAsCustomCollection<Feeds, FeedsCollection, FeedsMember, FeedsMemberCollection>();
            }
            catch (Exception e)
            {
                throw new Exception("An error has occured while generating a list of Feeds " + e.Message, e);
            }
        }

        /// <summary>
        /// Adds a new feed to the DB.
        /// </summary>
        /// <param name="feedUrl">The feed URL.</param>
        /// <param name="logoUrl">The logo URL.</param>
        /// <param name="categoryID">The category ID.</param>
        /// <returns></returns>
        /// <exception cref="System.Exception"></exception>
        public static int AddFeed(string feedUrl, string logoUrl, int categoryID)
        {
            FeedsMember feed = new FeedsMember();
            try
            {
                feed.FeedUrl = feedUrl;
                feed.LastUpdate = DateTime.Now;
                feed.LogoUrl = logoUrl;
                feed.CategoryID = categoryID;

                feed.Save();
            }
            catch (Exception e)
            {
                throw new Exception("An error has occured while inserting a new feed. " + e.Message);

            }
            return feed.Id;
        }

        /// <summary>
        /// Updates a feed.
        /// </summary>
        /// <param name="id">The id of the feed to update.</param>
        /// <param name="logoUrl">The logo URL.</param>
        /// <param name="pubDate">The pub date.</param>
        /// <returns></returns>
        /// <exception cref="System.Exception">An error has occured while updating a feed with id=+id+.  + e.Message</exception>
        public static Boolean UpdateFeed(int id, string logoUrl, DateTime pubDate)
        {
            Boolean ifUpdate = false;
            try
            {
                FeedsMember feed = RetrieveByID(id);

                if (feed != null)
                {
                    DB.Update<Feeds>()
                    .Set(Columns.LogoUrl).EqualTo(logoUrl)
                    .Set(Columns.LastUpdate).EqualTo(pubDate)
                    .Where(Columns.Id).IsEqualTo(id).Execute();
                    ifUpdate = true;
                }
            }
            catch (Exception e)
            {
                throw new Exception("An error has occured while updating a feed with id="+id+". " + e.Message);
            }
            return ifUpdate;
        }
    }

    public class FeedsMemberCollection : FeedsCollection
    {

    }
}
